using ILRuntime.Runtime.Intepreter;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ILTools
{
    [MenuItem("ILRuntime/Generate CLR Binding Code")]
    static void GenerateCLRBinding()
    {
        List<Type> types = new List<Type>();
        // 注册重定向函数
        types.Add(typeof(Dictionary<int, ILTypeInstance>));
        types.Add(typeof(Dictionary<int, int>));
        types.Add(typeof(Dictionary<object, object>));
        types.Add(typeof(Dictionary<int, object>));
        types.Add(typeof(Dictionary<long, object>));
        types.Add(typeof(Dictionary<long, int>));
        types.Add(typeof(Dictionary<int, long>));
        types.Add(typeof(Dictionary<string, long>));
        types.Add(typeof(Dictionary<string, int>));
        types.Add(typeof(Dictionary<string, object>));
        types.Add(typeof(Dictionary<string, GameObject>));
        types.Add(typeof(List<ILTypeInstance>));
        types.Add(typeof(List<int>));
        types.Add(typeof(List<long>));
        types.Add(typeof(List<string>));
        types.Add(typeof(List<object>));

        ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(types, "Assets/ILRuntime/Generated");

        AssetDatabase.Refresh();
    }

    [MenuItem("ILRuntime/Generate CLR Binding Code by Analysis")]
    static void GenerateCLRBindingByAnalysis()
    {
        //用新的分析热更dll调用引用来生成绑定代码
        ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain();
        using (System.IO.FileStream fs = new FileStream("Assets/GameRes/HotFixDll/Unity.Hotfix.dll.bytes", FileMode.Open, FileAccess.Read))
        {
            domain.LoadAssembly(fs);
            //Crossbind Adapter is needed to generate the correct binding code
            InitILRuntime(domain);
            ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, "Assets/ILRuntime/Generated");
        }
        
        AssetDatabase.Refresh();
    }

    static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain domain)
    {
        ////这里需要注册所有热更DLL中用到的跨域继承Adapter，否则无法正确抓取引用
        domain.RegisterCrossBindingAdaptor(new MonoBehaviourAdapter());
        domain.RegisterCrossBindingAdaptor(new CoroutineAdapter());
        domain.RegisterCrossBindingAdaptor(new InheritanceAdapter());
        //domain.RegisterCrossBindingAdaptor(new UIWindowAdapter());
    }


    /// <summary>
	/// 程序集路径
	/// </summary>
    const string AssembliesPath = "Hotfix_Project/bin/Debug";
    /// <summary>
	/// 发布目标路径
	/// </summary>
    const string BuildPath = "Assets/GameRes/HotFixDll";
    /// <summary>
	/// hotfixdll
	/// </summary>
    const string HotfixDll = "Hotfix_Project.dll";
    /// <summary>
	/// horfixpdb
	/// </summary>
    const string HotfixPdb = "Hotfix_Project.pdb";

    [MenuItem("ILRuntime/Create Hotfix DLL")]
    static void BuildHotfixDll()
    {
        string fixDll = Path.Combine(BuildPath, "Unity.Hotfix.dll.bytes");
        string fixPdb = Path.Combine(BuildPath, "Unity.Hotfix.pdb.bytes");

        File.WriteAllBytes(fixDll, File.ReadAllBytes(Path.Combine(AssembliesPath, HotfixDll)));
        File.WriteAllBytes(fixPdb, File.ReadAllBytes(Path.Combine(AssembliesPath, HotfixPdb)));

        AssetDatabase.Refresh();

        Debug.Log("程序集拷贝完成");
    }
}
